package com.mooral.web.action.voucher;

import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import com.mooral.bean.Voucher;
import com.mooral.context.Context;
import com.mooral.dataAccess.DataAccessConnection;
import com.mooral.web.action.MooralAction;

public class ViewAction extends MooralAction {

 	public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, Context context) throws Exception {

		  DynaActionForm dform = (DynaActionForm) form;
		  String slug = (String)dform.get("id");
		  
		  DataAccessConnection dataAccessConnection = context.getDataAccessConnection();

		  String sql = "select 	m.id as mid," +
			  		"						m.name as mname," +
			  		"						m.description as mdescription," +
			  		"						v.id as vid," +
			  		"						v.title as vtitle," +
			  		"						v.image as vimage," +
			  		"						v.type as vtype," +
			  		"						v.description as vdescription," +
			  		"						v.slug as vslug" +
			  		"  			from 		merchants m " +
			  		"			left join 	vouchers v" +
			  		"				on 		(m.id = v.merchant_id)" +
			  		"			where 		v.slug = '" + slug + "'";
		  
		  ResultSet resultSet = dataAccessConnection.getResultSetQuery(sql);
		  
		  
		  Voucher voucher = new Voucher();
		  if(resultSet.next()) voucher.populate(resultSet);
		  // TODO ver que hacer cuando no este mas este registro. Poco probable pero puedeo ocurrir
		  
		  dataAccessConnection.disposeConnection();
		  
		  request.setAttribute("voucher", voucher);
		  
		  String mname = voucher.get("mname").toString();
		  String vtitle = voucher.get("vtitle").toString();
		  String vtype = voucher.get("vtitle").toString();
		  request.setAttribute("headerTitle", mname + " " + vtype +" - " + vtitle + " when using this Promotion Code Discount Codes, Promotional Codes, Voucher Codes UK");
		  request.setAttribute("metaDescription", mname + " - " + vtitle + " when using this Promotion Code discounts, " + mname + " " + vtitle + " when using this Promotion Code Discount voucher codes, promotional codes and coupons for UK online stores. Save money with these free exclusive valid money off discount codes and special offers, check voucheap.com before you shop to find free discounts");
		  request.setAttribute("metaKeywords", mname + " " + vtitle + " Discount Code, Free Delivery Codes, Deals, Promotion, " + mname + " reviews coming soon");
		  
		  
		  return mapping.findForward(ACTION_SUCCESS);
 	}

}